"ui";

let bh = require("./module/bh")
const PROJECT = files.read("./project.json")


/**
 * 1. json驱动ui，便于维护
 * 2. 提供用户系统、悬浮窗开关
 * 3. 便于维护
 * 
 */


let bhlib = new bh()


bhlib.setDebug(true)


bhlib.setTabs(["首页", "设置", "我的"])

bhlib.init(JSON.parse(PROJECT))

let script_list = [
    {
        no: 1,
        title: "K28",
        desc: "如果怪物没有buff图标记得去改一下游戏画质！",
        script: "01.js",
        version: "0.2",
        count: bhlib.store.get("script_"+1) || 0
    },
    {
        no: 2,
        title: "个人挖土",
        desc: "一个人挖土有点枯燥...",
        script: "02.js",
        version: "0.2",
        count: bhlib.store.get("script_"+2) || 0
    },
    {
        no: 3,
        title: "刷妖气",
        desc: "你日和坊刷完没呢？",
        script: "03.js",
        version: "0.1",
        count: bhlib.store.get("script_"+3) || 0
    },
    {
        no: 4,
        title: "刷个人突破",
        desc: "勋章刷满没呢？",
        script: "04.js",
        version: "0.1",
        count: bhlib.store.get("script_"+4) || 0
    },
    {
        no: 5,
        title: "司机模式",
        desc: "记得去设置页设置一下队伍人数哦！",
        script: "05.js",
        version: "0.1",
        count: bhlib.store.get("script_"+5) || 0
    },
    {
        no: 6,
        title: "乘客模式",
        desc: "自动同意别人组队请求，自动战斗等...",
        script: "06.js",
        version: "0.1",
        count: bhlib.store.get("script_"+6) || 0
    },
    {
        no: 7,
        title: "过剧情",
        desc: "你还有剧情没过吗？",
        script: "07.js",
        version: "0.1",
        count: bhlib.store.get("script_"+7) || 0
    }
]


bhlib.run()


bhlib.addView(<vertical>
    <list id="home_list">
        <relative padding="8dp">
            <img tint="#000000" layout_centerVertical="true" id="icon" w="30dp" h="30dp" src="file://module/imgs/code.png"></img>
            <vertical w="*" marginLeft="6dp" gravity="center_vertical" layout_toRightOf="icon">
                <horizontal>
                    <text textSize="16sp" id="title" textColor="#000000" textStyle="bold" text="{{title}}"></text>
                    <text id="version" textSize="14sp" text="__Version:{{version}}"></text>
                </horizontal>
                <text id="count" text="已运行{{count}}次！" textSize="11sp"></text>
                <text textSize="13sp" marginTop="3dp" marginBottom="4dp" id="desc" text="{{desc}}"></text>

                <text id="no" visibility="gone" text="{{no}}"></text>
                <text text="" bg="#b2b2b2" h="1dp" w="*"></text>
            </vertical>
            <img tint="#000000" marginRight="16dp" layout_alignParentRight="true" layout_centerVertical="true" id="run_icon" w="30dp" h="30dp" src="file://module/imgs/bofang.png"></img>
        </relative>
    </list>
</vertical>, "首页", function () {
    ui.home_list.setDataSource(script_list)
    ui.home_list.on("item_click", function (item, i, itemView, listView) {
        if (bhlib.isroot) {
            let now_count = bhlib.store.get("script_"+item.no) || 0
            now_count++
            bhlib.store.put("script_"+item.no, now_count)
            toastLog(item.title)
            bhlib.runScriptFile("script/"+item.script)
        } else {
            toastLog("未激活")
        }
        
    })
})


bhlib.addView(<vertical>
    <card cardCornerRadius="15dp" cardElevation="3dp" w="*" h="auto" margin="6dp">
        <vertical>
            <text text="全局设置" w="*" bg="#b2b2b2" gravity="center" padding="4dp" textSize="18sp" textStyle="bold" textColor="#000000"></text>
            <vertical padding="3dp">
                <Switch padding='3dp' text="忽略电池优化" w="*" id="bh_super"></Switch>
                <Switch padding='3dp' text="战斗失败判断(关闭更流畅)" w="*" id="bh_tansuo_is_fail"></Switch>
                <horizontal>
                    <text text="启动等待："></text>
                    <input hint="默认5秒" w="*" inputType="number" id="bh_launch_time"></input>
                </horizontal>
            </vertical>
        </vertical>
    </card>
</vertical>, "设置", function () {
    print(ui.bh_tansuo_is_fail)
    
})

bhlib.addView(<vertical>
    <card cardCornerRadius="15dp" cardElevation="3dp" w="*" h="auto" margin="6dp">
        <vertical>
            <text text="探索设置" w="*" bg="#b2b2b2" gravity="center" padding="4dp" textSize="18sp" textStyle="bold" textColor="#000000"></text>
            <vertical padding="3dp">
                {/* <horizontal>
                    <text text="换狗粮："></text>
                    <spinner layout_weight="1" id="bh_tansuo_dogfood" entries="关闭|素材|N|R|SR"></spinner>
                </horizontal>
                <text textSize="12sp" text="队长默认左边第一个，其他两个位置为狗粮位"></text> */}
                <horizontal>
                    <text text="针对攻击："></text>
                    <spinner layout_weight="1" id="bh_tansuo_select_attack" entries="关闭|经验|金币|达摩"></spinner>
                </horizontal>
            </vertical>
        </vertical>
    </card>
</vertical>, "设置", function () {
    
})



bhlib.addView(<vertical>
    <card cardCornerRadius="15dp" cardElevation="3dp" w="*" h="auto" margin="6dp">
        <vertical>
            <text text="队长设置" w="*" bg="#b2b2b2" gravity="center" padding="4dp" textSize="18sp" textStyle="bold" textColor="#000000"></text>
            <vertical padding="3dp">
                <horizontal>
                    <text text="队伍人数："></text>
                    <spinner layout_weight="1" id="bh_teams_num" entries="2人|3人"></spinner>
                </horizontal>
            </vertical>
        </vertical>
    </card>
</vertical>, "设置", function () {
    
})


bhlib.addView(<vertical>
    <card cardCornerRadius="15dp" cardElevation="3dp" w="*" h="auto" margin="6dp">
        <vertical>
            <text text="个人中心" w="*" bg="#b2b2b2" gravity="center" padding="4dp" textSize="18sp" textStyle="bold" textColor="#000000"></text>
            <vertical padding="3dp">
                <horizontal>
                    <text text="用户："></text>
                    <text text={bhlib.pjysdk.getDeviceID()} id="user_id"></text>
                    
                </horizontal>
                <horizontal>
                    <text text="到期时间："></text>
                    <text text="null" id="user_endtime"></text>
                </horizontal>
                
                <horizontal>
                    <text text="卡密："></text>
                    <input layout_weight="1" id="bh_user_kami" text={bhlib.store.get("kami") || ""}></input>
                </horizontal>
                <horizontal>
                    <button layout_weight="1" text="登录" id="user_login"></button>
                    <button layout_weight="1" text="试用" id="user_test"></button>
                    <button layout_weight="1" text="充值" id="user_rech"></button>
                    <button layout_weight="1" text="退出" id="user_quit"></button>
                </horizontal>
            </vertical>
        </vertical>
    </card>
</vertical>, "我的", function () {
    // 自行登录
    threads.start(function(){
        let km = bhlib.store.get("kami")
        if (!km) {
            // 非登录状态
            ui.user_endtime.setText("未登录")
        } else {
            //登录
            bhlib.pjysdk.SetCard(km)
            let r = bhlib.pjysdk.CardLogin()
            print(r)
            if (r.message == "ok") {
                ui.run(function () {
                    ui.user_endtime.setText(r.result.expires)
                })
                bhlib.isroot = true
            } else {
                toastLog(r.message)
                ui.run(function () {
                    ui.user_endtime.setText(r.message)
                })
                bhlib.isroot = false
            }
        }
        
        ui.user_login.on("click", () => {
            let km__ = ui.bh_user_kami.getText()
            km__ = km__.toString()
            if (bhlib.isroot) {
                toastLog("重复登录")
                return
            }
            if (km__.length > 10) {
                bhlib.store.put("kami", km__)
                bhlib.pjysdk.SetCard(km__)
                let r = bhlib.pjysdk.CardLogin()
                if (r.message == "ok") {
                    ui.run(function () {
                        ui.user_endtime.setText(r.result.expires)
                    })
                    bhlib.isroot = true
                } else {
                    bhlib.isroot = false
                    toastLog(r.message)
                    ui.run(function () {
                        ui.user_endtime.setText(r.message)
                    })
                }
            } else {
                toastLog("输入卡密不合法")
            }
        })
    })
    
    ui.user_test.on("click", () => {
        threads.start(function () {
            bhlib.addgroup('57956771')
        })
    })
    
    ui.user_rech.on("click", () => {
        toastLog("暂不支持")
    })
    
    ui.user_quit.on("click", () => {
        threads.start(function (){
            let r = bhlib.pjysdk.CardLogout()
            toastLog("退出登录")
            exit()
        })
    })
    
})

ui.run(function(){
    bhlib.completion_view()
})